//
//  TTagButton.m
//  丁诚昊的外包项目(2016_V7宝)
//
//  Created by DimChum_Haul on 16/3/29.
//  Copyright © 2016年 沙罗双树831. All rights reserved.
//

#import "TTagButton.h"

/** 布局方式：~ 1.Masonry 2.CGRect */
#import "Masonry.h"
#import <objc/objc-runtime.h>
#import "UIColor+ColorFullTemplate.h"

@interface TTagButton ()
@property(weak) UIImageView* sub1;
@property(weak) UILabel* sub2;
@property (strong,nonatomic)UIColor* originColor;
@end

@implementation TTagButton
- (void) TestObjC_Runtime{
    /// 1.拿到自指针
    Class selfCls = object_getClass(self);
    unsigned int varListCount;
    Method *methodList = class_copyMethodList(selfCls, &varListCount);
    for (int i = 0; i< varListCount; i++) {
        Method method = methodList[i];
        SEL selector = method_getName(method);
        NSLog(@"--->%@",NSStringFromSelector(selector));
    }
}

- (void)blockCallee:(TTagButton*)sender {
    [sender setSelected:!sender.selected];
    
    NSString* identifier_Uniform = [NSString stringWithFormat:@"%p",sender];
    ActionAfterButtonClickedBlock block = [sender.blockQueue valueForKey:identifier_Uniform];
    if(block){
        block(sender,netStatusInit);
        [self TestObjC_Runtime];
    }else{
        NSLog(@"虽然实现起来复杂了一点 但是一定不会出现没有Action函数指针而崩程序的情况");
    }
}

- (void)setSelected:(BOOL)selected{
    [super setSelected:selected];
    
    if(!self.sub2) return;
    if (selected) {
        self.sub2.textColor = [UIColor GLColor];
    }else {
        self.sub2.textColor = _originColor;
    }
}

/** 生产方式: OOP继承UIButton  绑定事件响应者代码在内部 而非TTagButton的调用者addTarget[selector(xxx:)]*/
-(instancetype) initWithIcon:(NSString*)imgNamed
                       title:(NSString*)ButtonText
                 ActionBlock:(ActionAfterButtonClickedBlock)clickBlock {
    if(self = [super init]){
        UIImage* nameIMG = [UIImage imageNamed:imgNamed];
        UIImage* nameIMGHL = [UIImage imageNamed:[NSString stringWithFormat:@"%@+",imgNamed]];
        UIImageView* iconView = [[UIImageView alloc]initWithImage:nameIMG
                                                 highlightedImage:nameIMGHL];
        iconView.contentMode = UIViewContentModeScaleAspectFill;
        if(iconView.image == nil)
            NSLog(@"加载按钮图片失败。。。");
        
        self.originColor = [UIColor whiteColor];
        UILabel* titleLabel = [[UILabel alloc]init];
        titleLabel.text = ButtonText;
        titleLabel.font = [UIFont systemFontOfSize:11];;
        titleLabel.textColor = _originColor;
        
        [self addSubview:iconView];
        [self addSubview:titleLabel];
        
        [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(self).multipliedBy(0.4f);
            make.width.equalTo(iconView.mas_height);
            make.centerX.equalTo(self);
            make.centerY.equalTo(self).offset(-5);
        }];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.lessThanOrEqualTo(self.mas_bottom).offset(-1);
            make.centerX.equalTo(iconView);
            titleLabel.textAlignment = NSTextAlignmentCenter;
        }];
        
        ///取消背景颜色
        self.backgroundColor = [UIColor clearColor];
        
        /// 依赖注入代码 [一般人其实是看不懂这段代码的 这是老夫绝学！]
        [self addTarget:self
                 action:@selector(blockCallee:)
       forControlEvents:UIControlEventTouchUpInside];
        /// 1.创建字典
        _blockQueue = @{}.mutableCopy;
        if(clickBlock)
        {
            NSString* identifier_Uniform = [NSString stringWithFormat:@"%p",self];
            [_blockQueue setValue:[clickBlock copy] forKey:identifier_Uniform];
        }
        
        self.sub1 = iconView;
        self.sub2 = titleLabel;
        return self;
    }
    return nil;
}

/**
 * TODO: 这个与上一个方法的本质区别 多一个按钮中心图像UIImageView的高度Float值 而不是传入高度百分比
 * 并且这个方法不适用AutoLayoutOut 而是直接用CGRect绝对坐标系统创建按钮 事件绑定方式还是一样的:~ block
 */
-(instancetype) initWithWithICon:(NSString *)imgNamed
                           title:(NSString *)ButtonText
                     ActionBlock:(ActionAfterButtonClickedBlock)clickBlock
                      iconRadius:(CGFloat)perW
{
    self = [self initWithIcon:imgNamed title:ButtonText ActionBlock:clickBlock];
    if(self ){
        if (perW != 0) {
            UIImageView* ptr = (UIImageView*)self.subviews[0];
            [ptr mas_updateConstraints:^(MASConstraintMaker *make) {
                make.size.equalTo(perW);
                [self layoutIfNeeded];
            }];
        }
        return self;
    }
    return nil;
}

- (instancetype)initSimpleTTagBtnWithBackColor:(UIColor *)bgColor
                                     TitleText:(NSString *)title
                                   ActionBlock:(ActionAfterButtonClickedBlock)clickBlock
{
    if (self = [super init]) {
        self = [TTagButton buttonWithType:UIButtonTypeSystem];
        self.backgroundColor = bgColor;
        self.titleLabel.font = [UIFont fontWithName:@"Menio Regular" size:36];
        [self.titleLabel setAdjustsFontSizeToFitWidth:YES];

        [self setTitle:title forState:UIControlStateNormal];
        [self setTitle:@"请求中..." forState:UIControlStateSelected];
        
        [self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self setTitleColor:[UIColor SpecRandomColor] forState:UIControlStateSelected];
        [self setTintColor:[UIColor clearColor]];
        
        [self addTarget:self
                 action:@selector(blockCallee:)
       forControlEvents:UIControlEventTouchUpInside];
        /// 1.创建字典
        _blockQueue = @{}.mutableCopy;
        if(clickBlock)
        {
            NSString* identifier_Uniform = [NSString stringWithFormat:@"%p",self];
            [_blockQueue setValue:[clickBlock copy] forKey:identifier_Uniform];
        }
        return self;
    }
    return nil;
}
@end
